package com.gxa.autoparts.admin.service.impl;


import com.gxa.autoparts.admin.mapper.*;
import com.gxa.autoparts.admin.service.GoodsPropertiesService;
import com.gxa.autoparts.admin.service.GoodsService;
import com.gxa.autoparts.admin.service.SecondKillService;
import com.gxa.autoparts.core.pojo.Goods;
import com.gxa.autoparts.core.pojo.GoodsProperties;
import com.gxa.autoparts.core.pojo.GoodsType;
import com.gxa.autoparts.core.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("goodsService")
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService {
    @Resource
    GoodsMapper goodsMapper;
    @Resource
    GoodsTypeMapper goodsTypeMapper;
    @Resource
    GoodsImgMapper goodsImgMapper;
    @Resource
    GoodsSkuMapper goodsSkuMapper;
    @Resource
    KeyWordsMapper keyWordsMapper;
    @Resource
    GoodsPropertiesService goodsPropertiesService;
    @Autowired
    SecondKillService secondKillService;
    @Resource
    GoodsPropertiesMapper goodsPropertiesMapper;
    @Resource
    SecondKillMapper secondKillMapper;
    @Resource
    RushBuyMapper rushBuyMapper;
    @Override
    public List<Map> selectByPidAndGoodsName(Long pid, String name, Integer page, Integer size) throws Exception {
        //List<Goods> goods = goodsMapper.selectByPidAndGoodsName(pid, name, (page - 1) * size, size);
        /*if(goods==null){
            throw new Exception("没有该对应商品");
        }
        return goods;*/
        List<Map> maps = goodsMapper.selectByPidAndGoodsName(pid, name, page, size);
        if(maps==null){
            throw new Exception("没有查到数据");
        }
        return maps;
    }

    @Override
    public List<Goods> selectAllGoods(Integer page, Integer size) throws Exception {
        List<Goods> goods = goodsMapper.selectAllGoods((page - 1) * size, size);
        if(goods==null){
            throw new Exception("没有查到数据");
        }
        return goods;
    }

    @Override
    public GoodsType selectGoodsTypeByName(String name) throws Exception {
        GoodsType goodsType = goodsTypeMapper.selectGoodsTypeByName(name);
        if(goodsType==null){
            throw new Exception("没有查询到该数据");
        }
        return goodsType;
    }

    @Override
    public List<GoodsType> selectGoodsTypeById(Long id) throws Exception {
        List<GoodsType> goodsTypes = goodsTypeMapper.selectGoodsTypeById(id);
        if(goodsTypes==null){
            throw new Exception("没有查到数据");
        }
        return goodsTypes;
    }

    @Override
    public Map selectGoodsAllKind(Long id) {
        Goods goods = super.selectById(id);
        Map map = new HashMap();
        map.put("state",goods.getState());
        map.put("pid",goods.getTypeId());
        map.put("goodsName",goods.getName());
        map.put("img",goods.getImg());
        map.put("detail",goods.getDetail());
        List<Map> maps = goodsImgMapper.selectGoodsImgs(id);
        map.put("imgs",maps);
        List<Map> maps1 = goodsSkuMapper.selectGoodsSkus(id);
        map.put("sku",maps1);
        List<Map> maps2 = keyWordsMapper.selectKeyWordsById(id);
        map.put("keyWords",maps2);
        GoodsProperties goodsProperties = goodsPropertiesMapper.selectPropertiesByGoodsId(id);
        Boolean isNew = goodsProperties.getIsNew();
        Boolean isNormal = goodsProperties.getIsNormal();
        Boolean isSecondKill = goodsProperties.getIsSecondKill();
        Boolean isTimeLimit = goodsProperties.getIsTimeLimit();
        List list=new ArrayList();
        if(isNew){
            list.add("新品");
        }
        if(isNormal){
            list.add("普通商品");
        }
        if(isSecondKill){
            list.add("秒杀商品");
        }
        if(isTimeLimit){
            list.add("限时抢购商品");
        }
        map.put("properties",list);
        List<Map> maps3 = secondKillMapper.selectSecondKillByGoodsId(id);
        map.put("killTime",maps3);
        List<Map> maps4 = rushBuyMapper.selectRushBuyByGoodsId(id);
        map.put("shopTime",maps4);
        return  map;
    }
    /*{
        "state":"1",
        "pid":"1",
        "goodsName":"商品名字",
        "img":"封面图片",
        "imgs":
        [
        {"imgName":"第一张图片名字"},
        {"imgName":"第二章图片名字"}...
                ],
        "sku":[
        {"product":"{"颜色"："红色","尺寸":"10cm"}","count":"库存数量","price":"售价","markPrice":"市场价"},
        {"product":"{"颜色"："黑色","尺寸":"20cm"}","count":"库存数量","price":"售价","markPrice":"市场价"}...
            ],
        "keyWords":[
        {"keyName":"第一个关键词"},
        {"keyName":"第二个关键词"}...
                ],
        "properties":[
        {"proName":"第一个商品属性"},
        {"proName"："第二个商品属性"}....
                ],
        "killTime":[
        {"ktime":"第一个秒杀时间"},
        {"ktime":"第二个秒杀时间"}...
                ],
        "shopTime":[
        {"stime":"第一个抢购时间"},
        {"stime":"第二个抢购时间"}...
                ],
        "detail":"商品详情1"
            }*/
}
